package ca.unbc.cpsc672;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;

import ca.unbc.cpsc672.fuzzy.FuzzySet;
import ca.unbc.cpsc672.fuzzy.function.MembershipFunction;
import ca.unbc.cpsc672.plot.FunctionPlotWindow;

public class FuzzySetTab {
	private FunctionPlotWindow fuzPlot;
	private FuzzySet fuzSet;

	/**
	 * @param parentShell
	 */
	public FuzzySetTab(FuzzySet fuzSet) {
		this.fuzSet = fuzSet;
	}

	public Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout());
		buildPlotSection(composite);
		buildParaSection(composite);
		return composite;
	}

	private void buildPlotSection(Composite composite) {
		Group plotGroup = new Group(composite, SWT.NULL);
		plotGroup.setText("Plot");
		plotGroup.setLayout(new GridLayout());
		plotGroup.setLayoutData(GUIUtils.buildGridData());
		fuzPlot = new FunctionPlotWindow(plotGroup, fuzSet);
		fuzPlot.redrawPlot(fuzSet.getAllMembershipFunction().iterator().next()
				.getLinguisticVariable());
	}

	private void buildParaSection(Composite composite) {
		final CTabFolder parasfolder = new CTabFolder(composite, SWT.NONE);
		parasfolder.setLayoutData(GUIUtils.buildGridData());

		for (MembershipFunction f : fuzSet.getAllMembershipFunction()) {
			CTabItem ti = new CTabItem(parasfolder, SWT.NONE);
			ti.setText(f.getLinguisticVariable());
			ti.setControl(f.buildUI(parasfolder));
		}
		parasfolder.setSelection(0);
		parasfolder.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = parasfolder.getSelection().getText();
				fuzPlot.redrawPlot(name);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}
}